package com.zy.socket.chat2;

import java.io.DataOutputStream;
import java.net.Socket;
import java.util.Scanner;

/**
 * @Author Gong
 * @Description
 * @dATE 2021/12/19 16:13
 **/
public class Client {
    public static void main(String[] args) {
        System.out.println("这是接受控制台输入的客户端！！");
        //1 实例化客户端并连接服务端
        Socket client = null;
        try {
            client = new Socket("127.0.0.1",12345);
            System.out.println("连接服务器成功！");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("连接服务器失败！");
            return;
        }
        //2 连接成功后，先创建线程，不断从服务端读信息
        MyClientReceiveThread rt = new MyClientReceiveThread(client);
        rt.start();//启动线程，不断从服务端读取信息
        //3不断读取控制台内容
        DataOutputStream dos = null;
        try {
            dos = new DataOutputStream(client.getOutputStream());
            System.out.println("请输入：");
            Scanner sc = new Scanner(System.in);
            String readstr = sc.nextLine();
            while (!readstr.toUpperCase().equals("QUIT")) //只要读取的内容不为QUIT,则？？？
            {
                dos.writeUTF(readstr); //往服务器写
                sc.reset();
                readstr = sc.nextLine();//写完后，继续从控制台读
            }
        } catch (Exception e){

        }
    }
}
